<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>MySQL基础 | Joey</title><meta name="keywords" content="数据库,MySQL"><meta name="author" content="方陈勇"><meta name="copyright" content="方陈勇"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="MySQL基础整理">
<meta property="og:type" content="article">
<meta property="og:title" content="MySQL基础">
<meta property="og:url" content="https://fangchenyong.top/2020/02/10/%E6%95%B0%E6%8D%AE%E5%BA%93-MySQL-MySQL%E5%9F%BA%E7%A1%80/index.html">
<meta property="og:site_name" content="Joey">
<meta property="og:description" content="MySQL基础整理">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG">
<meta property="article:published_time" content="2020-02-09T16:00:00.000Z">
<meta property="article:modified_time" content="2020-05-22T10:00:04.039Z">
<meta property="article:author" content="方陈勇">
<meta property="article:tag" content="数据库">
<meta property="article:tag" content="MySQL">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://fangchenyong.top/2020/02/10/%E6%95%B0%E6%8D%AE%E5%BA%93-MySQL-MySQL%E5%9F%BA%E7%A1%80/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = { 
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2020-05-22 18:00:04'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/3FD9B055-6361-49B7-B8CE-5BA9144BD27F.JPG" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">43</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">51</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">53</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> List</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> Music</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> Movie</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">Joey</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> Home</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> Tags</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> Categories</span></a></div><div class="menus_item"><a class="site-page" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> List</span><i class="fas fa-chevron-down expand"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/music/"><i class="fa-fw fas fa-music"></i><span> Music</span></a></li><li><a class="site-page child" href="/movies/"><i class="fa-fw fas fa-video"></i><span> Movie</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">MySQL基础</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-02-09T16:00:00.000Z" title="发表于 2020-02-10 00:00:00">2020-02-10</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2020-05-22T10:00:04.039Z" title="更新于 2020-05-22 18:00:04">2020-05-22</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a><i class="fas fa-angle-right post-meta-separator"></i><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/MySQL/">MySQL</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">5.9k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>22分钟</span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="1-MySQL服务"><a href="#1-MySQL服务" class="headerlink" title="1.MySQL服务"></a>1.MySQL服务</h1><h2 id="1-1-MySQL服务的启动和停止"><a href="#1-1-MySQL服务的启动和停止" class="headerlink" title="1.1 MySQL服务的启动和停止"></a>1.1 MySQL服务的启动和停止</h2><p>方式一：计算机——右击管理——服务</p>
<p>方式二：通过管理员身份运行</p>
<ul>
<li><code>net start 服务名（启动服务）</code></li>
<li><code>net stop 服务名（停止服务）</code></li>
</ul>
<h2 id="1-2-MySQL服务的登录和退出"><a href="#1-2-MySQL服务的登录和退出" class="headerlink" title="1.2 MySQL服务的登录和退出"></a>1.2 MySQL服务的登录和退出</h2><p>登录：<strong>mysql 【-h 主机名 -P 端口号】 -u 用户名 -p密码</strong></p>
<p>退出：<strong>exit或ctrl+C</strong></p>
<h1 id="2-DQL-Data-Query-Language-数据查询语言"><a href="#2-DQL-Data-Query-Language-数据查询语言" class="headerlink" title="2.DQL(Data Query Language)数据查询语言"></a>2.DQL(Data Query Language)数据查询语言</h1><h2 id="2-1-基础查询"><a href="#2-1-基础查询" class="headerlink" title="2.1 基础查询"></a>2.1 基础查询</h2><p><strong>语法：</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT 查询列表 FROM 表名;</span><br></pre></td></tr></table></figure>

<p><strong>特点：</strong></p>
<ol>
<li>通过select查询完的结果 ，是一个虚拟的表格，不是真实存在</li>
<li>要查询的东西 可以是常量值、可以是表达式、可以是字段、可以是函数</li>
</ol>
<h2 id="2-2-条件查询"><a href="#2-2-条件查询" class="headerlink" title="2.2 条件查询"></a>2.2 条件查询</h2><p><strong>语法：</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT 查询列表 FROM 表名 WHERE 筛选条件;</span><br></pre></td></tr></table></figure>

<p><strong>分类：</strong></p>
<ol>
<li><p>条件表达式<br>示例：salary&gt;10000<br>条件运算符：&gt;、&lt; 、&gt;=、 &lt;=、 = 、!=、 &lt;&gt;</p>
</li>
<li><p>逻辑表达式</p>
<p>示例：salary&gt;10000 &amp;&amp; salary&lt;20000</p>
<p>逻辑运算符：</p>
<ul>
<li>and（&amp;&amp;）:两个条件如果同时成立，结果为true，否则为false</li>
<li>or(||)：两个条件只要有一个成立，结果为true，否则为false</li>
<li>not(!)：如果条件成立，则not后为false，否则为true</li>
</ul>
</li>
<li><p>模糊查询<br>示例：last_name like ‘a%’</p>
</li>
</ol>
<h2 id="2-3-排序查询"><a href="#2-3-排序查询" class="headerlink" title="2.3 排序查询"></a>2.3 排序查询</h2><p><strong>语法：</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT 查询列表 FROM 表名 ORDER BY 排序列表 【asc||desc】;</span><br></pre></td></tr></table></figure>

<p><strong>特点：</strong></p>
<ol>
<li><p>asc ：升序，如果不写默认升序</p>
<p>desc：降序</p>
</li>
<li><p>排序列表 支持 单个字段、多个字段、函数、表达式、别名</p>
</li>
<li><p>order by的位置一般放在查询语句的最后（除limit语句之外）</p>
</li>
</ol>
<h2 id="2-4-常见函数"><a href="#2-4-常见函数" class="headerlink" title="2.4 常见函数"></a>2.4 常见函数</h2><p><strong>语法：</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT 函数名(实参列表);</span><br></pre></td></tr></table></figure>

<p><strong>分类：</strong></p>
<ol>
<li><p>单行函数</p>
<ol>
<li><p>字符函数</p>
<ul>
<li><p>concat：连接</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT CONCAT(&#39;Hello&#39;,&#39;_&#39;,&#39;World&#39;);</span><br><span class="line">#结果：Hello_World</span><br></pre></td></tr></table></figure></li>
<li><p>substr：截取子串</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#注意：索引从1开始</span><br><span class="line">#截取从指定索引处后面所有字符</span><br><span class="line">SELECT SUBSTR(&#39;Hello World&#39;,7);</span><br><span class="line">#结果：World</span><br><span class="line">#截取从指定索引处指定字符长度的字符</span><br><span class="line">SELECT SUBSTR(&#39;Hello World&#39;,1,3);</span><br><span class="line">#结果：Hel</span><br></pre></td></tr></table></figure></li>
<li><p>upper：变大写</p>
   <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT UPPER(&#39;john&#39;);</span><br><span class="line">#结果：JOHN</span><br></pre></td></tr></table></figure></li>
<li><p>lower：变小写</p>
   <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">   SELECT LOWER(&#39;joHn&#39;);</span><br><span class="line">#结果：john</span><br></pre></td></tr></table></figure></li>
<li><p>replace：替换</p>
   <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT REPLACE(&#39;Hello World&#39;,&#39;l&#39;,&#39;i&#39;);</span><br><span class="line">#结果：Heiio Worid</span><br></pre></td></tr></table></figure></li>
<li><p>length：获取字节长度</p>
   <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT LENGTH(&#39;john&#39;);</span><br><span class="line">#结果：4</span><br></pre></td></tr></table></figure></li>
<li><p>trim：去前后空格</p>
   <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT LENGTH(TRIM(&#39;    Hello    &#39;));</span><br><span class="line">#结果：5</span><br></pre></td></tr></table></figure></li>
<li><p>lpad：左填充</p>
   <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT TRIM(&#39;l&#39; FROM &#39;llllllllHellolllll&#39;);</span><br><span class="line">#结果：Hello</span><br></pre></td></tr></table></figure></li>
<li><p>rpad：右填充</p>
   <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT LPAD(&#39;john&#39;,6,&#39;*&#39;);</span><br><span class="line">#结果：**john</span><br></pre></td></tr></table></figure></li>
<li><p>instr：获取子串第一次出现的索引</p>
   <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT INSTR(&#39;Hello World&#39;,&#39;o&#39;);</span><br><span class="line">#结果：5</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>数学函数</p>
<ul>
<li><p>ceil：向上取整</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT CEIL(-1.02);</span><br><span class="line">#结果：-1</span><br></pre></td></tr></table></figure></li>
<li><p>round：四舍五入</p>
 <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT ROUND(-1.55);</span><br><span class="line">#结果：-2</span><br><span class="line">SELECT ROUND(1.567,2);</span><br><span class="line">#结果：1.57</span><br></pre></td></tr></table></figure></li>
<li><p>mod：取模</p>
 <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT MOD(10,-3);</span><br><span class="line">#结果：1</span><br><span class="line">SELECT 10%3;</span><br><span class="line">#结果：1</span><br></pre></td></tr></table></figure></li>
<li><p>floor：向下取整</p>
 <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT FLOOR(-9.99);</span><br><span class="line">#结果：-10</span><br></pre></td></tr></table></figure></li>
<li><p>truncate：截断</p>
 <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT TRUNCATE(1.69999,1);</span><br><span class="line">#结果：1.6</span><br></pre></td></tr></table></figure></li>
<li><p>rand：获取随机数，返回0-1之间的小数</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT RAND();</span><br><span class="line">#结果：0.2870596892079894</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>日期函数</p>
<ul>
<li><p>now：返回当前日期+时间</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT NOW();</span><br><span class="line">#结果：2020-02-10 16:05:07</span><br></pre></td></tr></table></figure></li>
<li><p>curdate：返回当前日期</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT CURDATE();</span><br><span class="line">#结果：2020-02-10</span><br></pre></td></tr></table></figure></li>
<li><p>curtime：返回当前时间</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT CURTIME();</span><br><span class="line">#结果：16:05:07</span><br></pre></td></tr></table></figure></li>
<li><p>year：返回年</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT YEAR(NOW()) 年;</span><br><span class="line">#结果：2020</span><br><span class="line">SELECT YEAR(&#39;1998-1-1&#39;) 年;</span><br><span class="line">#结果：1998</span><br></pre></td></tr></table></figure></li>
<li><p>month：返回月</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT MONTH(NOW()) 月;</span><br><span class="line">#结果：2</span><br></pre></td></tr></table></figure></li>
<li><p>monthname：以英文形式返回月</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT MONTHNAME(NOW()) 月;</span><br><span class="line">#结果：February</span><br></pre></td></tr></table></figure></li>
<li><p>day：返回日</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT DAY(NOW()) 日;</span><br><span class="line">#结果：10</span><br></pre></td></tr></table></figure></li>
<li><p>hour：小时</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT HOUR(NOW()) 小时;</span><br><span class="line">#结果：16</span><br></pre></td></tr></table></figure></li>
<li><p>minute：分钟</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT MINUTE(NOW()) 分钟;</span><br><span class="line">#结果：6</span><br></pre></td></tr></table></figure></li>
<li><p>second：秒</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT SECOND(NOW()) 秒;</span><br><span class="line">#结果：50</span><br></pre></td></tr></table></figure></li>
<li><p>date_format：将日期转换成字符</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT DATE_FORMAT(NOW(),&#39;%y年%m月%d日&#39;);</span><br><span class="line">#结果：20年02月10日</span><br></pre></td></tr></table></figure></li>
<li><p>str_to_date：将字符转换成日期</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT STR_TO_DATE(&#39;1998-3-2&#39;,&#39;%Y-%c-%d&#39;);</span><br><span class="line">#结果：1998-03-02</span><br></pre></td></tr></table></figure></li>
<li><p>datediff：返回两个日期相差的天数</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT DATEDIFF(&#39;2020-12-30&#39;,&#39;2020-12-29&#39;);</span><br><span class="line">#结果：1</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>流程控制函数</p>
<ul>
<li><p><strong>if</strong>(条件表达式，表达式1，表达式2)：</p>
<p> 如果条件表达式成立，返回表达式1，否则返回表达式2</p>
  <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT IF(10&lt;5,&#39;大&#39;,&#39;小&#39;);</span><br><span class="line">#结果：小 </span><br></pre></td></tr></table></figure></li>
<li><p><strong>case情况1</strong></p>
  <figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">case 变量或表达式或字段</span><br><span class="line">when 常量1 then 值1</span><br><span class="line">when 常量2 then 值2</span><br><span class="line">...</span><br><span class="line">else 值n</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">#示例</span><br><span class="line">SELECT salary 原始工资,department_id,</span><br><span class="line">CASE department_id</span><br><span class="line">WHEN 30 THEN salary*1.1</span><br><span class="line">WHEN 40 THEN salary*1.2</span><br><span class="line">WHEN 50 THEN salary*1.3</span><br><span class="line">ELSE salary</span><br><span class="line">END AS 新工资</span><br><span class="line">FROM employees;</span><br></pre></td></tr></table></figure></li>
<li><p><strong>case情况2</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">case </span><br><span class="line">when 条件1 then 值1</span><br><span class="line">when 条件2 then 值2</span><br><span class="line">...</span><br><span class="line">else 值n</span><br><span class="line">end</span><br><span class="line"></span><br><span class="line">#示例</span><br><span class="line">SELECT salary,</span><br><span class="line">CASE </span><br><span class="line">	WHEN salary&gt;20000 THEN &#39;A&#39;</span><br><span class="line">    WHEN salary&gt;15000 THEN &#39;B&#39;</span><br><span class="line">	WHEN salary&gt;10000 THEN &#39;C&#39;</span><br><span class="line">    ELSE &#39;D&#39;</span><br><span class="line">END AS 工资级别</span><br><span class="line">FROM employees;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>其他函数</p>
<ul>
<li><p>version：当前数据库服务器的版本</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT VERSION();</span><br><span class="line">#结果：5.7.18-log</span><br></pre></td></tr></table></figure></li>
<li><p>database：当前打开的数据库</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT DATABASE();</span><br><span class="line">#结果：joeychat</span><br></pre></td></tr></table></figure></li>
<li><p>user：当前用户</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT USER();</span><br><span class="line">#结果：root@localhost</span><br></pre></td></tr></table></figure></li>
<li><p>password(‘字符’)：返回该字符的密码形式</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT PASSWORD(&#39;Hello&#39;);</span><br><span class="line">#结果：*FCA02337EEB51C3EE398B473FD9A9AFD093F9E64</span><br></pre></td></tr></table></figure></li>
<li><p>md5(‘字符’)：返回该字符的md5加密形式</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT MD5(&#39;Hello&#39;);</span><br><span class="line">#结果：8b1a9953c4611296a827abf8c47804d7    </span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ol>
</li>
<li><p>分组函数</p>
<ol>
<li><p>函数</p>
<ul>
<li><p>sum：求和</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT SUM(salary) FROM employees;</span><br></pre></td></tr></table></figure></li>
<li><p>max：最大值</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT MAX(salary) FROM employees;</span><br></pre></td></tr></table></figure></li>
<li><p>min：最小值</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT MIN(salary) FROM employees;</span><br></pre></td></tr></table></figure></li>
<li><p>avg：平均值</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT AVG(salary) FROM employees;</span><br></pre></td></tr></table></figure></li>
<li><p>count：计数</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT COUNT(salary) FROM employees;</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
<li><p>特点</p>
<ol>
<li>以上五个分组函数都忽略<code>null</code>值，除了count(*)</li>
<li>sum和avg一般用于处理数值型，max、min、count可以处理任何数据类型</li>
<li>可以搭配distinct使用，用于统计去重后的结果</li>
<li>count的参数可以支持：字段、*、常量值，一般放1</li>
<li>效率：<ul>
<li>MYISAM存储引擎下 ，COUNT(*)的效率高</li>
<li>INNODB存储引擎下，COUNT(*)和COUNT(1)的效率差不多，比COUNT(字段)要高一些</li>
</ul>
</li>
</ol>
</li>
</ol>
</li>
</ol>
<h2 id="2-5-分组查询"><a href="#2-5-分组查询" class="headerlink" title="2.5 分组查询"></a>2.5 分组查询</h2><p><strong>语法：</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT 查询列表 FROM 表名 GROUP BY 分组的字段 HAVING 分组后的筛选 ORDER BY 排序列表;</span><br></pre></td></tr></table></figure>

<p><strong>特点：</strong></p>
<ol>
<li><p>可以按单个字段分组</p>
</li>
<li><p>和分组函数一同查询的字段最好是分组后的字段</p>
</li>
<li><table>
<thead>
<tr>
<th>分组筛选</th>
<th>针对的表</th>
<th>位置</th>
<th>关键字</th>
</tr>
</thead>
<tbody><tr>
<td>分组前筛选</td>
<td>原始表</td>
<td>group by的前面</td>
<td>where</td>
</tr>
<tr>
<td>分组后筛选</td>
<td>分组后的结果集</td>
<td>group by的后面</td>
<td>having</td>
</tr>
</tbody></table>
</li>
<li><p>可以按多个字段分组，字段之间用逗号隔开</p>
</li>
<li><p>可以支持排序</p>
</li>
<li><p>having后可以支持别名</p>
</li>
</ol>
<h2 id="2-6-连接查询"><a href="#2-6-连接查询" class="headerlink" title="2.6 连接查询"></a>2.6 连接查询</h2><p><strong>sql99语法：</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT 字段，...</span><br><span class="line">FROM 表1</span><br><span class="line">INNER|LEFT OUTER|RIGHT OUTER|CROSS JOIN 表2 ON 连接条件</span><br><span class="line">INNER|LEFT OUTER|RIGHT OUTER|CROSS JOIN 表3 ON 连接条件</span><br><span class="line">WHERE 筛选条件</span><br><span class="line">GROUP BY 分组字段</span><br><span class="line">HAVING 分组后的筛选条件</span><br><span class="line">ORDER BY 排序的字段或表达式</span><br></pre></td></tr></table></figure>

<h2 id="2-7-子查询"><a href="#2-7-子查询" class="headerlink" title="2.7 子查询"></a>2.7 子查询</h2><p><strong>含义：</strong></p>
<p>​        一条查询语句中又嵌套了另一条完整的select语句，其中被嵌套的select语句，称为子查询或内查询。<br>​        在外面的查询语句，称为主查询或外查询。</p>
<p><strong>特点：</strong></p>
<ol>
<li><p>子查询都放在小括号内</p>
</li>
<li><p>子查询可以放在from后面、select后面、where后面、having后面，但一般放在条件的右侧</p>
</li>
<li><p>子查询优先于主查询执行，主查询使用了子查询的执行结果</p>
</li>
<li><p>子查询根据查询结果的行数不同分为以下两类：</p>
<ul>
<li><p>单行子查询</p>
<p>结果集只有一行，一般搭配单行操作符使用：&gt; &lt; = &lt;&gt; &gt;= &lt;= </p>
<p>非法使用子查询的情况：</p>
<ul>
<li>子查询的结果为一组值</li>
<li>子查询的结果为空</li>
</ul>
</li>
<li><p>多行子查询</p>
<p>结果集有多行，一般搭配多行操作符使用：any、all、in、not in</p>
<p>in： 属于子查询结果中的任意一个就行</p>
<p>any和all往往可以用其他查询代替</p>
</li>
</ul>
</li>
</ol>
<h2 id="2-8-分页查询"><a href="#2-8-分页查询" class="headerlink" title="2.8 分页查询"></a>2.8 分页查询</h2><p><strong>语法：</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT 查询列表</span><br><span class="line">FROM 表</span><br><span class="line">LIMIT offset,size;</span><br><span class="line">#offset代表的是起始的条目索引，默认从0开始,可省略</span><br><span class="line">#size代表的是显示的条目数</span><br></pre></td></tr></table></figure>

<p><strong>特点：</strong></p>
<ol>
<li><p>起始条目索引从0开始</p>
</li>
<li><p>limit子句放在查询语句的最后</p>
</li>
<li><p>公式：select * from  表 limit （page-1）*sizePerPage,sizePerPage，</p>
<p>说明：</p>
<p>​        sizePerPage：每页显示条目数</p>
<p>​        page：要显示的页数 </p>
</li>
</ol>
<h2 id="2-9-联合查询"><a href="#2-9-联合查询" class="headerlink" title="2.9 联合查询"></a>2.9 联合查询</h2><p><strong>语法：</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">SELECT 字段|常量|表达式|函数 FROM 表 WHERE 条件 UNION all</span><br><span class="line">SELECT 字段|常量|表达式|函数 FROM 表 WHERE 条件 UNION all</span><br><span class="line">SELECT 字段|常量|表达式|函数 FROM 表 WHERE 条件 UNION all</span><br><span class="line">.....</span><br><span class="line">SELECT 字段|常量|表达式|函数 FROM 表 WHERE 条件</span><br></pre></td></tr></table></figure>

<p><strong>特点：</strong></p>
<ol>
<li>多条查询语句的查询的列数必须是一致的</li>
<li>多条查询语句的查询的列的类型几乎相同</li>
<li><strong>union代表去重，union all代表不去重</strong></li>
</ol>
<h1 id="3-DML-Data-Manipulate-Language-数据操作语言"><a href="#3-DML-Data-Manipulate-Language-数据操作语言" class="headerlink" title="3. DML(Data Manipulate Language)数据操作语言"></a>3. DML(Data Manipulate Language)数据操作语言</h1><h2 id="3-1-插入"><a href="#3-1-插入" class="headerlink" title="3.1 插入"></a>3.1 插入</h2><p><strong>语法：</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#方式一</span><br><span class="line">INSERT INTO 表名(字段名，...)VALUES(值1，...);</span><br><span class="line">#方式二</span><br><span class="line">INSERT INTO 表名 SET 字段&#x3D;值,字段&#x3D;值,...;</span><br></pre></td></tr></table></figure>

<p><strong>特点：</strong></p>
<ol>
<li>字段类型和值类型一致或兼容，而且一一对应</li>
<li>可以为空的字段，可以不用插入值，或用null填充</li>
<li>不可以为空的字段，必须插入值</li>
<li>字段个数和值的个数必须一致</li>
<li>字段可以省略，但默认所有字段，并且顺序和表中的存储顺序一致</li>
</ol>
<h2 id="3-2-修改"><a href="#3-2-修改" class="headerlink" title="3.2 修改"></a>3.2 修改</h2><p><strong>语法：</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#单表更新</span><br><span class="line">UPDATE 表名 SET 字段&#x3D;值,字段&#x3D;值 WHERE 筛选条件;</span><br><span class="line">#多表更新</span><br><span class="line">UPDATE 表1 别名 </span><br><span class="line">LEFT|RIGHT|INNER JOIN 表2 别名 </span><br><span class="line">ON 连接条件  </span><br><span class="line">SET 字段&#x3D;值,字段&#x3D;值 </span><br><span class="line">WHERE 筛选条件;</span><br></pre></td></tr></table></figure>

<h2 id="3-3-删除"><a href="#3-3-删除" class="headerlink" title="3.3 删除"></a>3.3 删除</h2><p><strong>语法：</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#使用DELETE</span><br><span class="line"> #一、删除单表的记录</span><br><span class="line"> DELETE FROM 表名 WHERE 筛选条件;</span><br><span class="line"> #二、级联删除</span><br><span class="line"> DELETE 别名1,别名2 FROM 表1 别名 </span><br><span class="line"> LEFT|RIGHT|INNER JOIN 表2 别名 </span><br><span class="line"> ON 连接条件</span><br><span class="line"> WHERE 筛选条件;</span><br><span class="line">#使用TRUNCATE</span><br><span class="line">	TRUNCATE TABLE 表名;</span><br><span class="line">#区别：</span><br><span class="line">#	1.truncate删除后，如果再插入，标识列从1开</span><br><span class="line">#	  delete删除后，如果再插入，标识列从断点开始</span><br><span class="line">#	2.delete可以添加筛选条件</span><br><span class="line">#     truncate不可以添加筛选条件</span><br><span class="line">#	3.truncate效率较高</span><br><span class="line">#	4.truncate没有返回值</span><br><span class="line">#	  delete可以返回受影响的行数</span><br><span class="line">#	5.truncate不可以回滚</span><br><span class="line">#	  delete可以回滚</span><br></pre></td></tr></table></figure>

<h1 id="4-DDL-Data-Define-Language-数据定义语言"><a href="#4-DDL-Data-Define-Language-数据定义语言" class="headerlink" title="4.DDL(Data Define Language)数据定义语言"></a>4.DDL(Data Define Language)数据定义语言</h1><h2 id="4-1-库的管理"><a href="#4-1-库的管理" class="headerlink" title="4.1 库的管理"></a>4.1 库的管理</h2><ol>
<li><p>创建库</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE DATABASE 【IF NOT EXISTS】 库名【 CHARACTER SET 字符集名】</span><br></pre></td></tr></table></figure></li>
<li><p>修改库</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER DATABASE 库名 CHARACTER SET 字符集名;</span><br></pre></td></tr></table></figure></li>
<li><p>删除库</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">DROP DATABASE 【IF EXISTS】 库名;</span><br></pre></td></tr></table></figure></li>
</ol>
<h2 id="4-2-表的管理"><a href="#4-2-表的管理" class="headerlink" title="4.2 表的管理"></a>4.2 表的管理</h2><ol>
<li><p>创建表</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE TABLE IF NOT EXISTS stuinfo(</span><br><span class="line">	stuId INT,</span><br><span class="line">	stuName VARCHAR(20),</span><br><span class="line">	gender CHAR,</span><br><span class="line">	bornDate DATETIME</span><br><span class="line">);</span><br></pre></td></tr></table></figure></li>
<li><p>修改表</p>
<p><strong>语法：</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 ADD|MODIFY|DROP|CHANGE COLUMN 字段名 【字段类型】;</span><br></pre></td></tr></table></figure>

<ol>
<li><p>添加字段</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 ADD COLUMN 列名 类型 【First|After 字段名】;</span><br></pre></td></tr></table></figure></li>
<li><p>修改字段名</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 CHANGE COLUMN 旧列名 新列名 类型;</span><br></pre></td></tr></table></figure></li>
<li><p>修改字段的类型或约束</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 MODIFY COLUMN 列名 新类型 【新约束】;</span><br></pre></td></tr></table></figure></li>
<li><p>删除列</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 DROP COLUMN 列名;</span><br></pre></td></tr></table></figure></li>
<li><p>修改表名</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 RENAME 【to】 新表名;</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
<li><p>删除表</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">DROP TABLE [IF EXISTS] studentinfo;</span><br></pre></td></tr></table></figure></li>
<li><p>复制表</p>
<ol>
<li><p>复制表结构</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE TABLE 表名 LIKE 旧表;</span><br></pre></td></tr></table></figure></li>
<li><p>复制表结构+数据</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE TABLE 表名</span><br><span class="line">SELECT 查询列表 FROM 旧表 【WHERE筛选】;</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
</ol>
<h2 id="4-3-数据类型"><a href="#4-3-数据类型" class="headerlink" title="4.3 数据类型"></a>4.3 数据类型</h2><ol>
<li><p>整型</p>
<table>
<thead>
<tr>
<th align="left">类型</th>
<th align="left">大小</th>
<th align="left">范围（有符号）</th>
<th align="left">范围（无符号）</th>
<th align="left">用途</th>
</tr>
</thead>
<tbody><tr>
<td align="left">TINYINT</td>
<td align="left">1 byte</td>
<td align="left">(-128，127)</td>
<td align="left">(0，255)</td>
<td align="left">小整数值</td>
</tr>
<tr>
<td align="left">SMALLINT</td>
<td align="left">2 bytes</td>
<td align="left">(-32 768，32 767)</td>
<td align="left">(0，65 535)</td>
<td align="left">大整数值</td>
</tr>
<tr>
<td align="left">MEDIUMINT</td>
<td align="left">3 bytes</td>
<td align="left">(-8 388 608，8 388 607)</td>
<td align="left">(0，16 777 215)</td>
<td align="left">大整数值</td>
</tr>
<tr>
<td align="left">INT或INTEGER</td>
<td align="left">4 bytes</td>
<td align="left">(-2 147 483 648，2 147 483 647)</td>
<td align="left">(0，4 294 967 295)</td>
<td align="left">大整数值</td>
</tr>
<tr>
<td align="left">BIGINT</td>
<td align="left">8 bytes</td>
<td align="left">(-9,223,372,036,854,775,808，9 223 372 036 854 775 807)</td>
<td align="left">(0，18 446 744 073 709 551 615)</td>
<td align="left">极大整数值</td>
</tr>
<tr>
<td align="left">FLOAT</td>
<td align="left">4 bytes</td>
<td align="left">(-3.402 823 466 E+38，-1.175 494 351 E-38)，0，(1.175 494 351 E-38，3.402 823 466 351 E+38)</td>
<td align="left">0，(1.175 494 351 E-38，3.402 823 466 E+38)</td>
<td align="left">单精度 浮点数值</td>
</tr>
<tr>
<td align="left">DOUBLE</td>
<td align="left">8 bytes</td>
<td align="left">(-1.797 693 134 862 315 7 E+308，-2.225 073 858 507 201 4 E-308)，0，(2.225 073 858 507 201 4 E-308，1.797 693 134 862 315 7 E+308)</td>
<td align="left">0，(2.225 073 858 507 201 4 E-308，1.797 693 134 862 315 7 E+308)</td>
<td align="left">双精度 浮点数值</td>
</tr>
<tr>
<td align="left">DECIMAL</td>
<td align="left">对DECIMAL(M,D) ，如果M&gt;D，为M+2否则为D+2</td>
<td align="left">依赖于M和D的值</td>
<td align="left">依赖于M和D的值</td>
<td align="left">小数值</td>
</tr>
</tbody></table>
</li>
<li><p>浮点型</p>
<table>
<thead>
<tr>
<th>类型</th>
<th>DECIMAL(M,D)</th>
<th>FLOAT</th>
<th>DOUBLE</th>
</tr>
</thead>
<tbody><tr>
<td>长度</td>
<td>对DECIMAL(M,D) ，如果M&gt;D，为M+2否则为D+2</td>
<td>4</td>
<td>8</td>
</tr>
</tbody></table>
</li>
<li><p>字符型</p>
<table>
<thead>
<tr>
<th align="left">类型</th>
<th align="left">大小</th>
<th align="left">用途</th>
</tr>
</thead>
<tbody><tr>
<td align="left">CHAR</td>
<td align="left">0-255 bytes</td>
<td align="left">定长字符串</td>
</tr>
<tr>
<td align="left">VARCHAR</td>
<td align="left">0-65535 bytes</td>
<td align="left">变长字符串</td>
</tr>
<tr>
<td align="left">TINYBLOB</td>
<td align="left">0-255 bytes</td>
<td align="left">不超过 255 个字符的二进制字符串</td>
</tr>
<tr>
<td align="left">TINYTEXT</td>
<td align="left">0-255 bytes</td>
<td align="left">短文本字符串</td>
</tr>
<tr>
<td align="left">BLOB</td>
<td align="left">0-65 535 bytes</td>
<td align="left">二进制形式的长文本数据</td>
</tr>
<tr>
<td align="left">TEXT</td>
<td align="left">0-65 535 bytes</td>
<td align="left">长文本数据</td>
</tr>
<tr>
<td align="left">MEDIUMBLOB</td>
<td align="left">0-16 777 215 bytes</td>
<td align="left">二进制形式的中等长度文本数据</td>
</tr>
<tr>
<td align="left">MEDIUMTEXT</td>
<td align="left">0-16 777 215 bytes</td>
<td align="left">中等长度文本数据</td>
</tr>
<tr>
<td align="left">LONGBLOB</td>
<td align="left">0-4 294 967 295 bytes</td>
<td align="left">二进制形式的极大文本数据</td>
</tr>
<tr>
<td align="left">LONGTEXT</td>
<td align="left">0-4 294 967 295 bytes</td>
<td align="left">极大文本数据</td>
</tr>
</tbody></table>
</li>
<li><p>日期型</p>
<table>
<thead>
<tr>
<th align="left">类型</th>
<th align="left">大小 ( bytes)</th>
<th align="left">范围</th>
<th align="left">格式</th>
<th align="left">用途</th>
</tr>
</thead>
<tbody><tr>
<td align="left">DATE</td>
<td align="left">3</td>
<td align="left">1000-01-01/9999-12-31</td>
<td align="left">YYYY-MM-DD</td>
<td align="left">日期值</td>
</tr>
<tr>
<td align="left">TIME</td>
<td align="left">3</td>
<td align="left">‘-838:59:59’/‘838:59:59’</td>
<td align="left">HH:MM:SS</td>
<td align="left">时间值或持续时间</td>
</tr>
<tr>
<td align="left">YEAR</td>
<td align="left">1</td>
<td align="left">1901/2155</td>
<td align="left">YYYY</td>
<td align="left">年份值</td>
</tr>
<tr>
<td align="left">DATETIME</td>
<td align="left">8</td>
<td align="left">1000-01-01 00:00:00/9999-12-31 23:59:59</td>
<td align="left">YYYY-MM-DD HH:MM:SS</td>
<td align="left">混合日期和时间值</td>
</tr>
<tr>
<td align="left">TIMESTAMP</td>
<td align="left">4</td>
<td align="left">1970-01-01 00:00:00/2038结束时间是第 <strong>2147483647</strong> 秒，北京时间 <strong>2038-1-19 11:14:07</strong>，格林尼治时间 2038年1月19日 凌晨 03:14:07</td>
<td align="left">YYYYMMDD HHMMSS</td>
<td align="left">混合日期和时间值，时间戳</td>
</tr>
</tbody></table>
</li>
</ol>
<h2 id="4-4-常见的约束"><a href="#4-4-常见的约束" class="headerlink" title="4.4 常见的约束"></a>4.4 常见的约束</h2><ol>
<li><p>常见约束</p>
<ol>
<li>NOT NULL：非空，该字段的值必填</li>
<li>UNIQUE：唯一，该字段的值不可重复</li>
<li>DEFAULT：默认，该字段的值不用手动插入有默认值</li>
<li>CHECK：检查，MYSQL不支持</li>
<li>PRIMARY KEY：主键，该字段的值不可重复并且非空  NIQUE+NOT NULL</li>
<li>FOREIGN KEY：外键，该字段的值引用了另外的表的字段</li>
</ol>
<p><strong>其中，主键和唯一</strong></p>
<ol>
<li>区别：<ul>
<li>一个表至多有一个主键，但可以有多个唯一</li>
<li>主键不允许为空，唯一可以为空</li>
</ul>
</li>
<li>相同点<ul>
<li>都具有唯一性</li>
<li>都支持组合键，但不推荐</li>
</ul>
</li>
</ol>
<p><strong>外键</strong></p>
<ol>
<li><p>用于限制两个表的关系，从表的字段值引用了主表的某字段值</p>
</li>
<li><p>外键列和主表的被引用列要求类型一致，意义一样，名称无要求</p>
</li>
<li><p>主表的被引用列要求是一个KEY（一般就是主键）</p>
</li>
<li><p>插入数据，先插入主表</p>
</li>
<li><p>当设置有外键的时候，可以通过一下两种方式实现删除主表的记录：</p>
<ul>
<li><p><strong>方式一：级联删除</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">alter table stuinfo add CONSTRAINT fk_stu_major FOREIGN KEY(majorid) REFERENCES major(id) on DELETE CASCADE;</span><br></pre></td></tr></table></figure>

<p>说明：这样删除主表中的数据的时候，引用他外键的表的数据也都会进行删除，当然这种做法有点残忍，但是在实际的业务中，进行数据维护的时候，确实也有这么干的</p>
</li>
<li><p><strong>方式二：级联置空</strong></p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">alter table stuinfo add CONSTRAINT fk_stu_major FOREIGN KEY(majorid) REFERENCES major(id) on DELETE set null;</span><br></pre></td></tr></table></figure>

<p><strong>说明：</strong>这样删除主表中的数据的时候，引用他外键的表的数据不会进行删除，对应的引用外键的那一列会置为空，这种方法比较好。<br><strong>综上所述，要结合具体的业务场景进行选择。</strong></p>
</li>
</ul>
</li>
</ol>
</li>
<li><p>创建表时添加约束</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE TABLE 表名(</span><br><span class="line">	字段名 字段类型 NOT NULL,#非空</span><br><span class="line">	字段名 字段类型 PRIMARY KEY,#主键</span><br><span class="line">	字段名 字段类型 UNIQUE,#唯一</span><br><span class="line">	字段名 字段类型 DEFAULT 值,#默认</span><br><span class="line">	CONSTRAINT 约束名 FOREIGN KEY(字段名) REFERENCES 主表（被引用列）</span><br><span class="line">)</span><br></pre></td></tr></table></figure>

<p><strong>注意：</strong></p>
<p>列级约束可以在一个字段上追加多个，中间用空格隔开，没有顺序要求</p>
<p><strong>对比：</strong></p>
<table>
<thead>
<tr>
<th></th>
<th>支持类型</th>
<th>可以起约束名</th>
</tr>
</thead>
<tbody><tr>
<td>列级约束</td>
<td>除了外键</td>
<td>不可以</td>
</tr>
<tr>
<td>表级约束</td>
<td>除了非空和默认</td>
<td>可以，但对主键无效</td>
</tr>
</tbody></table>
</li>
<li><p>修改表时添加或删除约束</p>
<ol>
<li><p>非空</p>
<ol>
<li><p>添加非空</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 MODIFY COLUMN 字段名 字段类型 NOT NULL;</span><br></pre></td></tr></table></figure></li>
<li><p>删除非空</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 MODIFY COLUMN 字段名 字段类型 ;</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
<li><p>默认</p>
<ol>
<li><p>添加默认</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 MODIFY COLUMN 字段名 字段类型 DEFAULT 值;</span><br></pre></td></tr></table></figure></li>
<li><p>删除默认</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 MODIFY COLUMN 字段名 字段类型 ;</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
<li><p>主键</p>
<ol>
<li><p>添加主键</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 ADD【 CONSTRAINT 约束名】 PRIMARY KEY(字段名);</span><br></pre></td></tr></table></figure></li>
<li><p>删除主键</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 DROP PRIMARY KEY;</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
<li><p>唯一</p>
<ol>
<li><p>添加唯一</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 ADD【 CONSTRAINT 约束名】 UNIQUE(字段名);</span><br></pre></td></tr></table></figure></li>
<li><p>删除唯一</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 DROP INDEX 索引名;</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
<li><p>外键</p>
<ol>
<li><p>添加外键</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 ADD【 CONSTRAINT 约束名】 FOREIGN KEY(字段名) REFERENCES 主表（被引用列）;</span><br></pre></td></tr></table></figure></li>
<li><p>删除外键</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表名 DROP FOREIGN KEY 约束名;</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
</ol>
</li>
<li><p>自增长列</p>
<ol>
<li><p>特点：</p>
<ol>
<li><p>不用手动插入值，可以自动提供序列值，默认从1开始，步长为1</p>
<p>AUTO_INCREMENT_INCREMENT</p>
<p>如果要更改起始值：手动插入值</p>
<p>如果要更改步长：更改系统变量 SET AUTO_INCREMENT_INCREMENT=值;</p>
</li>
<li><p>一个表至多有一个自增长列</p>
</li>
<li><p>自增长列只能支持数值型</p>
</li>
<li><p>自增长列必须为一个KEY</p>
</li>
</ol>
</li>
<li><p>创建表时设置自增长列</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">CREATE TABLE 表(</span><br><span class="line">	字段名 字段类型 约束 AUTO_INCREMENT</span><br><span class="line">)</span><br></pre></td></tr></table></figure></li>
<li><p>修改表时设置自增长列</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表 MODIFY COLUMN 字段名 字段类型 约束 AUTO_INCREMENT</span><br></pre></td></tr></table></figure></li>
<li><p>删除自增长列</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">ALTER TABLE 表 MODIFY COLUMN 字段名 字段类型 约束 </span><br></pre></td></tr></table></figure></li>
</ol>
</li>
</ol>
<h1 id="5-TCL-Transaction-Control-Language-事务控制语言"><a href="#5-TCL-Transaction-Control-Language-事务控制语言" class="headerlink" title="5.TCL(Transaction Control Language)事务控制语言"></a>5.TCL(Transaction Control Language)事务控制语言</h1><h2 id="5-1含义"><a href="#5-1含义" class="headerlink" title="5.1含义"></a>5.1含义</h2><p><strong>事务：</strong></p>
<p>​        一条或多条sql语句组成一个执行单位，一组sql语句要么都执行要么都不执行</p>
<h2 id="5-2-特点（ACID）"><a href="#5-2-特点（ACID）" class="headerlink" title="5.2 特点（ACID）"></a>5.2 特点（ACID）</h2><ul>
<li>A 原子性：一个事务是不可再分割的整体，要么都执行要么都不执行</li>
<li>C 一致性：一个事务可以使数据从一个一致状态切换到另外一个一致的状态</li>
<li>I 隔离性：一个事务不受其他事务的干扰，多个事务互相隔离的</li>
<li>D 持久性：一个事务一旦提交了，则永久的持久化到本地</li>
</ul>
<h2 id="5-3-使用步骤"><a href="#5-3-使用步骤" class="headerlink" title="5.3 使用步骤"></a>5.3 使用步骤</h2><p><strong>了解：</strong><br>隐式（自动）事务：没有明显的开启和结束，本身就是一条事务可以自动提交，比如insert、update、delete<br>显式事务：具有明显的开启和结束</p>
<p><strong>使用显式事务：</strong></p>
<p>①开启事务<br>set autocommit=0;<br>start transaction;#可以省略</p>
<p>②编写一组逻辑sql语句<br>注意：sql语句支持的是insert、update、delete</p>
<p>设置回滚点：<br>savepoint 回滚点名;</p>
<p>③结束事务<br>提交：commit;<br>回滚：rollback;<br>回滚到指定的地方：rollback to 回滚点名;</p>
<h2 id="5-4-并发事务"><a href="#5-4-并发事务" class="headerlink" title="5.4 并发事务"></a>5.4 并发事务</h2><ol>
<li><p>事务的并发问题是如何发生的？<br>多个事务同时操作 同一个数据库的相同数据时</p>
</li>
<li><p>并发问题都有哪些？<br>脏读：一个事务读取了其他事务还没有提交的数据，读到的是其他事务“更新”的数据<br>不可重复读：一个事务多次读取，结果不一样<br>幻读：一个事务读取了其他事务还没有提交的数据，只是读到的是 其他事务“插入”的数据</p>
</li>
<li><p>如何解决并发问题<br>通过设置隔离级别来解决并发问题</p>
</li>
<li><p>隔离级别</p>
<table>
<thead>
<tr>
<th></th>
<th>脏读</th>
<th>不可重复读</th>
<th>幻读</th>
</tr>
</thead>
<tbody><tr>
<td>READ UNCOMMITTED:读未提交</td>
<td>×</td>
<td>×</td>
<td>×</td>
</tr>
<tr>
<td>READ COMMITTED：读已提交</td>
<td>√</td>
<td>×</td>
<td>×</td>
</tr>
<tr>
<td>REPEATABLE READ：可重复读</td>
<td>√</td>
<td>√</td>
<td>×</td>
</tr>
<tr>
<td>SERIALIZABLE：串行化</td>
<td>√</td>
<td>√</td>
<td>√</td>
</tr>
</tbody></table>
</li>
<li><p>设置和查看隔离级别</p>
<ol>
<li><p>设置隔离级别</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">set session|global  transaction isolation level 隔离级别名;</span><br></pre></td></tr></table></figure></li>
<li><p>查看隔离级别</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">select @@tx_isolation;</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
</ol>
<h1 id="6-其他"><a href="#6-其他" class="headerlink" title="6.其他"></a>6.其他</h1><h2 id="6-1-视图"><a href="#6-1-视图" class="headerlink" title="6.1 视图"></a>6.1 视图</h2><ol>
<li><p>含义：</p>
<p>​        mysql5.1版本出现的新特性，本身是一个虚拟表，它的数据来自于表，通过执行时动态生成。</p>
</li>
<li><p>好处：</p>
<ol>
<li>简化sql语句</li>
<li>提高了sql的重用性</li>
<li>保护基表的数据，提高了安全性</li>
</ol>
</li>
<li><p>语法</p>
<ol>
<li><p>创建</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">create view 视图名</span><br><span class="line">as</span><br><span class="line">查询语句;</span><br></pre></td></tr></table></figure></li>
<li><p>修改</p>
<ol>
<li><p>方式一</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">create or replace view 视图名</span><br><span class="line">as</span><br><span class="line">查询语句;</span><br></pre></td></tr></table></figure></li>
<li><p>方式二</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">alter view 视图名</span><br><span class="line">as</span><br><span class="line">查询语句;</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
<li><p>删除</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">drop view 视图1，视图2,...;</span><br></pre></td></tr></table></figure></li>
<li><p>查看</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">desc 视图名;</span><br><span class="line">show create view 视图名;</span><br></pre></td></tr></table></figure></li>
<li><p>使用</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">1.插入</span><br><span class="line">insert</span><br><span class="line">2.修改</span><br><span class="line">update</span><br><span class="line">3.删除</span><br><span class="line">delete</span><br><span class="line">4.查看</span><br><span class="line">select</span><br></pre></td></tr></table></figure></li>
<li><p>注意</p>
<p>视图一般用于查询的，而不是更新的，所以具备以下特点的视图都不允许更新<br>①包含分组函数、group by、distinct、having、union、<br>②join<br>③常量视图<br>④where后的子查询用到了from中的表<br>⑤用到了不可更新的视图</p>
</li>
<li><p>视图和表的对比</p>
<table>
<thead>
<tr>
<th></th>
<th>关键字</th>
<th>是否占用物理空间</th>
<th>使用</th>
</tr>
</thead>
<tbody><tr>
<td>视图</td>
<td>view</td>
<td>占用较小，只保存sql逻辑</td>
<td>一般用于查询</td>
</tr>
<tr>
<td>表</td>
<td>table</td>
<td>保存实际的数据</td>
<td>增删改查</td>
</tr>
</tbody></table>
</li>
</ol>
</li>
</ol>
<h2 id="6-2-变量"><a href="#6-2-变量" class="headerlink" title="6.2 变量"></a>6.2 变量</h2><ol>
<li><p>系统变量</p>
<p><strong>说明：</strong>变量由系统提供的，不用自定义。</p>
<ol>
<li><p>全局变量</p>
<p>服务器层面上的，必须拥有super权限才能为系统变量赋值，作用域为整个服务器，也就是针对于所有连接（会话）有效</p>
</li>
<li><p>会话变量</p>
<p>服务器为每一个连接的客户端都提供了系统变量，作用域为当前的连接（会话）</p>
</li>
</ol>
<p><strong>语法：</strong></p>
<ul>
<li><p>查看系统变量</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#如果没有显式声明global还是session，则默认是session</span><br><span class="line">show 【global|session 】variables like &#39;&#39;; </span><br></pre></td></tr></table></figure></li>
<li><p>查看指定的系统变量的值</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#如果没有显式声明global还是session，则默认是session</span><br><span class="line">select @@【global|session】.变量名; </span><br></pre></td></tr></table></figure></li>
<li><p>为系统变量赋值</p>
<ul>
<li><p>方式一</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#如果没有显式声明global还是session，则默认是session</span><br><span class="line">set 【global|session 】 变量名&#x3D;值; </span><br></pre></td></tr></table></figure></li>
<li><p>方式二</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">set @@global.变量名&#x3D;值;</span><br><span class="line">set @@变量名&#x3D;值；</span><br></pre></td></tr></table></figure></li>
</ul>
</li>
</ul>
</li>
<li><p>自定义变量</p>
<ol>
<li><p>用户变量</p>
<p>作用域：针对于当前连接（会话）生效</p>
<p>位置：begin end里面，也可以放在外面</p>
<p>使用：</p>
<ol>
<li><p>声明并赋值</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">set @变量名&#x3D;值;或</span><br><span class="line">set @变量名:&#x3D;值;或</span><br><span class="line">select @变量名:&#x3D;值;</span><br></pre></td></tr></table></figure></li>
<li><p>更新值</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">set @变量名&#x3D;值;或</span><br><span class="line">set @变量名:&#x3D;值;或</span><br><span class="line">select @变量名:&#x3D;值;</span><br><span class="line">select xx into @变量名 from 表;</span><br></pre></td></tr></table></figure></li>
<li><p>使用</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">select @变量名;</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
<li><p>局部变量</p>
<p>作用域：仅仅在定义它的begin end中有效</p>
<p>位置：只能放在begin end中，而且只能放在第一句</p>
<p>使用：</p>
<ol>
<li><p>声明</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">declare 变量名 类型 【default 值】;</span><br></pre></td></tr></table></figure></li>
<li><p>赋值或更新</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">set 变量名&#x3D;值;或</span><br><span class="line">set 变量名:&#x3D;值;或</span><br><span class="line">select @变量名:&#x3D;值;</span><br><span class="line">select xx into 变量名 from 表;</span><br></pre></td></tr></table></figure></li>
<li><p>使用</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">select 变量名;</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
</ol>
</li>
</ol>
<h2 id="6-3-存储过程"><a href="#6-3-存储过程" class="headerlink" title="6.3 存储过程"></a>6.3 存储过程</h2><ol>
<li><p>创建</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">create procedure 存储过程名(参数模式 参数名 参数类型)</span><br><span class="line">begin</span><br><span class="line">		存储过程体</span><br><span class="line">end</span><br></pre></td></tr></table></figure>

<p><strong>注意：</strong></p>
<p><strong>1.参数模式：in、out、inout，其中in可以省略</strong></p>
<p><strong>2.存储过程体的每一条sql语句都需要用分号结尾</strong></p>
</li>
<li><p>调用</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">call 存储过程名(实参列表)</span><br><span class="line">#举例：</span><br><span class="line">#调用in模式的参数：call sp1（‘值’）;</span><br><span class="line">#调用out模式的参数：set @name; call sp1(@name);select @name;</span><br><span class="line">#调用inout模式的参数：set @name&#x3D;值; call sp1(@name); select @name;</span><br></pre></td></tr></table></figure></li>
<li><p>查看</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">show create procedure 存储过程名;</span><br></pre></td></tr></table></figure></li>
<li><p>删除</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">drop procedure 存储过程名;</span><br></pre></td></tr></table></figure></li>
</ol>
<h2 id="6-4-函数"><a href="#6-4-函数" class="headerlink" title="6.4 函数"></a>6.4 函数</h2><ol>
<li><p>创建</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">create function 函数名(参数名 参数类型) returns  返回类型</span><br><span class="line">begin</span><br><span class="line">	函数体</span><br><span class="line">end</span><br></pre></td></tr></table></figure>

<p><strong>注意：函数体中肯定需要有return语句</strong></p>
</li>
<li><p>调用</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">select 函数名(实参列表);</span><br></pre></td></tr></table></figure></li>
<li><p>查看</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">show create function 函数名;</span><br></pre></td></tr></table></figure></li>
<li><p>删除</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">drop function 函数名;</span><br></pre></td></tr></table></figure></li>
</ol>
<h2 id="6-5-流程控制结构"><a href="#6-5-流程控制结构" class="headerlink" title="6.5 流程控制结构"></a>6.5 流程控制结构</h2><h3 id="6-5-1-分支结构"><a href="#6-5-1-分支结构" class="headerlink" title="6.5.1 分支结构"></a>6.5.1 分支结构</h3><ol>
<li><p>IF函数</p>
<p>功能：实现简单双分支</p>
<p>语法：</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">if(条件，值1，值2)</span><br></pre></td></tr></table></figure>

<p>位置：可以作为表达式放在任何位置</p>
</li>
<li><p>CASE结构</p>
<p>功能：实现多分支</p>
<p>语法：</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">#第一种</span><br><span class="line">case 表达式或字段</span><br><span class="line">when 值1 then 语句1;</span><br><span class="line">when 值2 then 语句2；</span><br><span class="line">..</span><br><span class="line">else 语句n;</span><br><span class="line">end [case];</span><br><span class="line">#第二种</span><br><span class="line">case </span><br><span class="line">when 条件1 then 语句1;</span><br><span class="line">when 条件2 then 语句2；</span><br><span class="line">..</span><br><span class="line">else 语句n;</span><br><span class="line">end [case];</span><br></pre></td></tr></table></figure></li>
<li><p>IF结构</p>
<p>功能：实现多分支</p>
<p>语法：</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">if 条件1 then 语句1;</span><br><span class="line">elseif 条件2 then 语句2;</span><br><span class="line">...</span><br><span class="line">else 语句n;</span><br><span class="line">end if;</span><br></pre></td></tr></table></figure>

<p>位置：只能放在begin end中</p>
</li>
</ol>
<h3 id="6-5-2-循环结构"><a href="#6-5-2-循环结构" class="headerlink" title="6.5.2 循环结构"></a>6.5.2 循环结构</h3><ol>
<li><p>WHILE</p>
<p>语法：</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">【名称:】while 循环条件 do</span><br><span class="line">		循环体</span><br><span class="line">end while 【名称】;</span><br></pre></td></tr></table></figure></li>
<li><p>LOOP</p>
<p>语法：</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">【名称：】loop</span><br><span class="line">		循环体</span><br><span class="line">end loop 【名称】;</span><br></pre></td></tr></table></figure></li>
<li><p>REPEAT</p>
<p>语法：</p>
<figure class="highlight plain"><table><tr><td class="code"><pre><span class="line">【名称:】repeat</span><br><span class="line">		循环体</span><br><span class="line">until 结束条件 </span><br><span class="line">end repeat 【名称】;</span><br></pre></td></tr></table></figure></li>
<li><p>对比</p>
<ol>
<li><p>这三种循环都可以省略名称，但如果循环中添加了循环控制语句（leave或iterate）则必须添加名称</p>
<ul>
<li>leave：类似于break，用于跳出所在的循环</li>
<li>iterate：类似于continue，用于结束本次循环，继续下一次</li>
</ul>
</li>
<li><p>loop 一般用于实现简单的死循环</p>
<p>while 先判断后执行</p>
<p>repeat 先执行后判断，无条件至少执行一次</p>
</li>
</ol>
</li>
</ol>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">方陈勇</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://fangchenyong.top/2020/02/10/%E6%95%B0%E6%8D%AE%E5%BA%93-MySQL-MySQL%E5%9F%BA%E7%A1%80/">https://fangchenyong.top/2020/02/10/%E6%95%B0%E6%8D%AE%E5%BA%93-MySQL-MySQL%E5%9F%BA%E7%A1%80/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://fangchenyong.top" target="_blank">Joey</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a><a class="post-meta__tags" href="/tags/MySQL/">MySQL</a></div><div class="post_share"><div class="social-share" data-image="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2020/04/01/%E4%B8%AD%E9%97%B4%E4%BB%B6-Redis/"><img class="prev-cover" src="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Redis命令</div></div></a></div><div class="next-post pull-right"><a href="/2019/12/19/%E5%9C%B0%E5%9B%BE-%E9%AB%98%E5%BE%B7%E5%9C%B0%E5%9B%BE%E7%94%BB%E7%82%B9%E3%80%81%E6%8A%98%E7%BA%BF%E3%80%81%E5%A4%9A%E8%BE%B9%E5%BD%A2%E3%80%81%E9%95%BF%E6%96%B9%E5%BD%A2%E3%80%81%E5%9C%86/"><img class="next-cover" src="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">高德地图绘制矢量图</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2020/04/19/数据库-MySQL-MySQL高级/" title="MySQL高级"><img class="cover" src="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2020-04-19</div><div class="title">MySQL高级</div></div></a></div><div><a href="/2019/07/23/数据库-Oracle-Oracle常用操作/" title="Oracle常用操作"><img class="cover" src="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-07-23</div><div class="title">Oracle常用操作</div></div></a></div><div><a href="/2019/07/23/数据库-Sql Server-sqlserver存储过程/" title="SqlServer存储过程使用游标循环"><img class="cover" src="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-07-23</div><div class="title">SqlServer存储过程使用游标循环</div></div></a></div><div><a href="/2019/07/23/数据库-Sql Server-sqlserver数据库备份还原/" title="SqlServer 备份还原"><img class="cover" src="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2019-07-23</div><div class="title">SqlServer 备份还原</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/3FD9B055-6361-49B7-B8CE-5BA9144BD27F.JPG" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">方陈勇</div><div class="author-info__description">一路向前</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">43</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">51</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">53</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/fangchenyong"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/fangchenyong" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:1013659102@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">个人笔记，如有疑问请联系 QQ:1013659102。</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#1-MySQL%E6%9C%8D%E5%8A%A1"><span class="toc-number">1.</span> <span class="toc-text">1.MySQL服务</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#1-1-MySQL%E6%9C%8D%E5%8A%A1%E7%9A%84%E5%90%AF%E5%8A%A8%E5%92%8C%E5%81%9C%E6%AD%A2"><span class="toc-number">1.1.</span> <span class="toc-text">1.1 MySQL服务的启动和停止</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#1-2-MySQL%E6%9C%8D%E5%8A%A1%E7%9A%84%E7%99%BB%E5%BD%95%E5%92%8C%E9%80%80%E5%87%BA"><span class="toc-number">1.2.</span> <span class="toc-text">1.2 MySQL服务的登录和退出</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#2-DQL-Data-Query-Language-%E6%95%B0%E6%8D%AE%E6%9F%A5%E8%AF%A2%E8%AF%AD%E8%A8%80"><span class="toc-number">2.</span> <span class="toc-text">2.DQL(Data Query Language)数据查询语言</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#2-1-%E5%9F%BA%E7%A1%80%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.1.</span> <span class="toc-text">2.1 基础查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-2-%E6%9D%A1%E4%BB%B6%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.2.</span> <span class="toc-text">2.2 条件查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-3-%E6%8E%92%E5%BA%8F%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.3.</span> <span class="toc-text">2.3 排序查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-4-%E5%B8%B8%E8%A7%81%E5%87%BD%E6%95%B0"><span class="toc-number">2.4.</span> <span class="toc-text">2.4 常见函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-5-%E5%88%86%E7%BB%84%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.5.</span> <span class="toc-text">2.5 分组查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-6-%E8%BF%9E%E6%8E%A5%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.6.</span> <span class="toc-text">2.6 连接查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-7-%E5%AD%90%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.7.</span> <span class="toc-text">2.7 子查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-8-%E5%88%86%E9%A1%B5%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.8.</span> <span class="toc-text">2.8 分页查询</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#2-9-%E8%81%94%E5%90%88%E6%9F%A5%E8%AF%A2"><span class="toc-number">2.9.</span> <span class="toc-text">2.9 联合查询</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#3-DML-Data-Manipulate-Language-%E6%95%B0%E6%8D%AE%E6%93%8D%E4%BD%9C%E8%AF%AD%E8%A8%80"><span class="toc-number">3.</span> <span class="toc-text">3. DML(Data Manipulate Language)数据操作语言</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#3-1-%E6%8F%92%E5%85%A5"><span class="toc-number">3.1.</span> <span class="toc-text">3.1 插入</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-2-%E4%BF%AE%E6%94%B9"><span class="toc-number">3.2.</span> <span class="toc-text">3.2 修改</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#3-3-%E5%88%A0%E9%99%A4"><span class="toc-number">3.3.</span> <span class="toc-text">3.3 删除</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#4-DDL-Data-Define-Language-%E6%95%B0%E6%8D%AE%E5%AE%9A%E4%B9%89%E8%AF%AD%E8%A8%80"><span class="toc-number">4.</span> <span class="toc-text">4.DDL(Data Define Language)数据定义语言</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#4-1-%E5%BA%93%E7%9A%84%E7%AE%A1%E7%90%86"><span class="toc-number">4.1.</span> <span class="toc-text">4.1 库的管理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-2-%E8%A1%A8%E7%9A%84%E7%AE%A1%E7%90%86"><span class="toc-number">4.2.</span> <span class="toc-text">4.2 表的管理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-3-%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">4.3.</span> <span class="toc-text">4.3 数据类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#4-4-%E5%B8%B8%E8%A7%81%E7%9A%84%E7%BA%A6%E6%9D%9F"><span class="toc-number">4.4.</span> <span class="toc-text">4.4 常见的约束</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#5-TCL-Transaction-Control-Language-%E4%BA%8B%E5%8A%A1%E6%8E%A7%E5%88%B6%E8%AF%AD%E8%A8%80"><span class="toc-number">5.</span> <span class="toc-text">5.TCL(Transaction Control Language)事务控制语言</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#5-1%E5%90%AB%E4%B9%89"><span class="toc-number">5.1.</span> <span class="toc-text">5.1含义</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-2-%E7%89%B9%E7%82%B9%EF%BC%88ACID%EF%BC%89"><span class="toc-number">5.2.</span> <span class="toc-text">5.2 特点（ACID）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-3-%E4%BD%BF%E7%94%A8%E6%AD%A5%E9%AA%A4"><span class="toc-number">5.3.</span> <span class="toc-text">5.3 使用步骤</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#5-4-%E5%B9%B6%E5%8F%91%E4%BA%8B%E5%8A%A1"><span class="toc-number">5.4.</span> <span class="toc-text">5.4 并发事务</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#6-%E5%85%B6%E4%BB%96"><span class="toc-number">6.</span> <span class="toc-text">6.其他</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#6-1-%E8%A7%86%E5%9B%BE"><span class="toc-number">6.1.</span> <span class="toc-text">6.1 视图</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-2-%E5%8F%98%E9%87%8F"><span class="toc-number">6.2.</span> <span class="toc-text">6.2 变量</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-3-%E5%AD%98%E5%82%A8%E8%BF%87%E7%A8%8B"><span class="toc-number">6.3.</span> <span class="toc-text">6.3 存储过程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-4-%E5%87%BD%E6%95%B0"><span class="toc-number">6.4.</span> <span class="toc-text">6.4 函数</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#6-5-%E6%B5%81%E7%A8%8B%E6%8E%A7%E5%88%B6%E7%BB%93%E6%9E%84"><span class="toc-number">6.5.</span> <span class="toc-text">6.5 流程控制结构</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#6-5-1-%E5%88%86%E6%94%AF%E7%BB%93%E6%9E%84"><span class="toc-number">6.5.1.</span> <span class="toc-text">6.5.1 分支结构</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#6-5-2-%E5%BE%AA%E7%8E%AF%E7%BB%93%E6%9E%84"><span class="toc-number">6.5.2.</span> <span class="toc-text">6.5.2 循环结构</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2021/05/06/%E7%AE%97%E6%B3%95-%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F/" title="算法"><img src="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="算法"/></a><div class="content"><a class="title" href="/2021/05/06/%E7%AE%97%E6%B3%95-%E6%8F%92%E5%85%A5%E6%8E%92%E5%BA%8F/" title="算法">算法</a><time datetime="2021-05-05T16:00:00.000Z" title="发表于 2021-05-06 00:00:00">2021-05-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/05/06/%E7%AE%97%E6%B3%95-%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F/" title="选择排序"><img src="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="选择排序"/></a><div class="content"><a class="title" href="/2021/05/06/%E7%AE%97%E6%B3%95-%E9%80%89%E6%8B%A9%E6%8E%92%E5%BA%8F/" title="选择排序">选择排序</a><time datetime="2021-05-05T16:00:00.000Z" title="发表于 2021-05-06 00:00:00">2021-05-06</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/04/12/Java-%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3Java%E8%99%9A%E6%8B%9F%E6%9C%BA/" title="深入理解Java虚拟机"><img src="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="深入理解Java虚拟机"/></a><div class="content"><a class="title" href="/2021/04/12/Java-%E6%B7%B1%E5%85%A5%E7%90%86%E8%A7%A3Java%E8%99%9A%E6%8B%9F%E6%9C%BA/" title="深入理解Java虚拟机">深入理解Java虚拟机</a><time datetime="2021-04-11T16:00:00.000Z" title="发表于 2021-04-12 00:00:00">2021-04-12</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/03/21/%E9%9D%A2%E8%AF%95-%E5%B9%B6%E5%8F%91%E3%80%81%E5%A4%9A%E7%BA%BF%E7%A8%8B/" title="面试题-并发编程"><img src="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="面试题-并发编程"/></a><div class="content"><a class="title" href="/2021/03/21/%E9%9D%A2%E8%AF%95-%E5%B9%B6%E5%8F%91%E3%80%81%E5%A4%9A%E7%BA%BF%E7%A8%8B/" title="面试题-并发编程">面试题-并发编程</a><time datetime="2021-03-20T16:00:00.000Z" title="发表于 2021-03-21 00:00:00">2021-03-21</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2021/03/20/%E9%9D%A2%E8%AF%95-%E9%9B%86%E5%90%88/" title="面试题-集合框架"><img src="https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="面试题-集合框架"/></a><div class="content"><a class="title" href="/2021/03/20/%E9%9D%A2%E8%AF%95-%E9%9B%86%E5%90%88/" title="面试题-集合框架">面试题-集合框架</a><time datetime="2021-03-19T16:00:00.000Z" title="发表于 2021-03-20 00:00:00">2021-03-20</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://fangchenyong.oss-cn-hangzhou.aliyuncs.com/BEF238F4E59CF4D91A694FE9C5DBC030.JPG')"><div id="footer-wrap"><div class="copyright">&copy;2019 - 2021 By 方陈勇</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">人生没有退路！</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"></div><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script></div></body></html>